<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
	String otype = (String)request.getAttribute("otype");
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>收货地址管理</title>
		<jsp:include page="/page/jsp/member/head.jsp" />
		<script type="text/javascript">
			var otype = "<%=otype%>";
			
			//根据所选省的code找到所有parent为此code的数据
			function getCitys(){
				var selectedIndex = document.getElementById("province").selectedIndex;
				var code = document.getElementById("province")[selectedIndex].value;
				if(code == "0"){   //表示什么都没有选中
					document.getElementById("city").innerHTML = "----请选择市----";
				}else{
					var type = "city";
					var postURL = path+"/member/initCityCounty.htm";
					$.ajax({
					   type: "POST",
					   url: postURL,
					   data: "type="+type+"&code="+code,
					   success: resultHandle
					});
				}
			}
			
			function resultHandle(result)
			{
				var res = result.split("|");
				var city = document.getElementById("city");
				city.options.length = 0;
				city.options.add(new Option());
				for(var i=0;i<res.length;i++){
					var op = new Option(res[i].split(",")[1],res[i].split(",")[0]);
					city.options.add(op);
				}
			}
			
			//根据所选市code找到所有parent为此code的数据
			function getCountys(){
				var selectedIndex = document.getElementById("city").selectedIndex;
				var code = document.getElementById("city")[selectedIndex].value;
				if(code == "0"){
					document.getElementById("region").innerHTML = "----请选择县区----";
				}else{
					var type = "county";
					var postURL = path+"/member/initCityCounty.htm";
					$.ajax({
					   type: "POST",
					   url: postURL,
					   data: "type="+type+"&code="+code,
					   success: resultHandle1
					});
				}
			}
			
			function resultHandle1(result)
			{
				var res = result.split("|");
				var region = document.getElementById("region");
				region.options.length = 0;
				for(var i=0;i<res.length;i++){
					var op = new Option(res[i].split(",")[1],res[i].split(",")[0]);
					region.options.add(op);
				}
			}
			
			//点击编辑数据添加到编辑中
			function edit(id){
				if(id!=""){
					window.location = path+"/member/address.htm?otype=edit&addressId="+id;
				}
			}
			
			//添加或者保存修改的地址
			function save(id){
				var name = escape(escape($('#name').val()));
				var tel = escape(escape($('#tel').val()));
				var province = escape(escape($('#province option:selected').text()));
				var city = escape(escape($('#city option:selected').text()));
				var region = escape(escape($('#region option:selected').text()));
				var address = escape(escape($('#address').val()));
				var zipcode = escape(escape($('#zipcode').val()));
				var isdefault = "";
				if(document.getElementById("checkbox").checked==true){   //如果选择
					isdefault = "1";
				}else{
					isdefault = "0";
				}
				
				if(moibleExits(tel) == "1"){  //表示验证通过
					var url = "";
					if(otype == "edit"){
						url = path+"/member/updateAddress.htm?id="+id+"&name="+name+"&tel="+tel+"&province="+province+"&city="+city+"&region="+region+"&address="+address+"&zipcode="+zipcode+"&isdefault="+isdefault;
					}else{
						url = path+"/member/save.htm?name="+name+"&tel="+tel+"&province="+province+"&city="+city+"&region="+region+"&address="+address+"&zipcode="+zipcode+"&isdefault="+isdefault;
					}
					$.ajax({
						   url: url,
						   success: function(data){
							   if(data=="1"){
								   window.location = path+"/member/address.htm?otype=add";
							   }else{
								   alertBox("操作失败!");
							   }
						   }
					});
				}else{
					alertBox("电话号码格式不对");
				}
				
				
				
			}
			
			//删除
			function deleteAddress(id){
				if(id!=""){
					$.ajax({
						   url: path+"/member/delete.htm?id="+id,
						   success: function(data){
							   if(data=="1"){
								   window.location = path+"/member/address.htm?otype=add";
							   }else{
								   alertBox("操作失败!");
							   }
						   }
					});
					
				}
			}
			
			
			function moibleExits(tel){  // /^0[0-9]\d{2,3}\-[2-9]\d{1}[0-9]\d{6,7}$/.test(tel)
				if(/^1[3|4|5|8][0-9]\d{8}$/.test(tel)){ 
					return "1";								
				}else{
					return "0";
				}
			}
			
			
			
			
		</script>
	</head>
<body>
	<div class="right2">
	<h1><b>收货地址管理</b></h1>
		<div class="ri_main2">
		  <table width="95%" id="mgrTable" border="0" cellpadding="8" cellspacing="1" class="margintop10" style="background:#d9d9d9;">
		    <tr>
		      <td width="7%" bgcolor="#eeeeee"><b>序号</b></td>
		      <td width="12%" bgcolor="#eeeeee"><b>收件人</b></td>
		      <td width="15%" bgcolor="#eeeeee"><b>联系电话</b></td>
		      <td width="45%" bgcolor="#eeeeee"><b>收货地址</b></td>
		      <td width="20%" bgcolor="#eeeeee"><b>操作</b></td>
		    </tr>
		    <c:forEach var="address" items="${list}" varStatus="s">
				    <tr>
				      <td bgcolor="#FFFFFF"><c:out value="${s.count}" /></td>
				      <td bgcolor="#FFFFFF">${address.getName()}</td>
				      <td bgcolor="#FFFFFF">${address.getTel()}</td>
				      <td bgcolor="#FFFFFF">${address.getAddressAll()}</td>
				      <td bgcolor="#FFFFFF">
				      	<input type="button" onclick="edit(${address.getId()})" value="编辑" />
				        <input type="button" onclick="deleteAddress(${address.getId()})" value="删除" />
				      </td>
				    </tr>
		    </c:forEach>
		    </table>
		  <table width="95%" border="0" cellpadding="8" cellspacing="1" class="margintop10" style="background:#d9d9d9;">
		    <tr>
		      <td colspan="2" align="center" bgcolor="#EEEEEE" class="f14 redfont"><b>增加收货地址</b></td>
		      </tr>
		    <tr>
		      <td width="19%" align="right" bgcolor="#FFFFFF">
		      	<span class="redfont">*</span>收货人姓名：
		      </td>
		      <td width="81%" bgcolor="#FFFFFF">
		       	<label for="textfield4"></label>
				<input type="text"  name="name" id="name" value="${address!=null?address.name:''}" />
			  </td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF"><span class="redfont">*</span>收货人电话：</td>
		      <td bgcolor="#FFFFFF">
		      	<input type="text" name="tel" id="tel" value="${address!=null?address.tel:''}" />
		      	<a href="#"></a>
		      </td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF"><span class="redfont">*</span>送货地址：</td>
		      <td bgcolor="#FFFFFF"><label for="select5"></label>
		        <select name="province" id="province" onchange="getCitys()">
		          <option>----请选择省份----</option>
	          		<c:forEach items="${provinces}" var="province" varStatus="pstatus">
						<c:choose>
							<c:when test="${!empty address && address.province==province.name}"><option value="${province.code }" selected="selected">${province.name }</option></c:when>
							<c:otherwise><option value="${province.code }">${province.name }</option></c:otherwise>
						</c:choose>
			  		</c:forEach>
		        </select>
		        
		        <select name="city" id="city" onchange="getCountys()">
		         	<c:if test="${!empty address}">
						<option value="${address.city }" selected="selected">${address.city }</option>
					</c:if>
		        </select>
		        
		        <select name="region" id="region">
		          	<c:if test="${!empty address}">
						<option value="${address.region }" selected="selected">${address.region }</option>
					</c:if>
		        </select></td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF"><span class="redfont">*</span>街道地址：</td>
		      <td bgcolor="#FFFFFF"><input type="text" name="address" id="address" value="${address!=null?address.address:''}"/></td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF"><span class="redfont">*</span>邮政编码：</td>
		      <td bgcolor="#FFFFFF"><input type="text" name="zipcode" id="zipcode"  value="${address!=null?address.zipcode:''}"/></td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF">是否为默认地址：</td>
		      <td bgcolor="#FFFFFF">
		      	<c:choose>
		      		<c:when test="${address!=null && address.isdefault==1}">
		      			<input name="checkbox" type="checkbox" id="checkbox" checked="checked" />
		      		</c:when>
		      		<c:otherwise>
		      			<input name="checkbox" type="checkbox" id="checkbox" />
		      		</c:otherwise>
		      	</c:choose>
		      </td>
		    </tr>
		    <tr>
		      <td align="right" bgcolor="#FFFFFF">&nbsp;</td>
		      <td bgcolor="#FFFFFF"><input type="button" onclick="save(${address!=null?address.id:''})" value="增加地址" />
		    </tr>
		  </table>
		</div>
	</div>
</body>
</html>